package com.zhuang.io.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ChannelTest {
	public static void main(String[] args) throws Exception {
		// write();
		// read();
		// copy();
		
	}

	public static void write() {
		try {
			// 1、字节输出流通向目标文件
			FileOutputStream fos = new FileOutputStream("data01.txt");
			// 2、得到字节输出流对应的通道Channel
			FileChannel channel = fos.getChannel();
			// 3、分配缓冲区
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			String s = "kangxiaozhuang";
			buffer.put(s.getBytes());
			// 4、把缓冲区切换成写出模式
			buffer.flip();
			channel.write(buffer);
			channel.close();
			System.out.println("写数据到文件中！");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void read() throws Exception {
		// 1、定义一个文件字节输入流与源文件接通
		FileInputStream fis = new FileInputStream("data01.txt");
		// 2、需要得到文件字节输入流的文件通道
		FileChannel channel = fis.getChannel();
		// 3、定义一个缓冲区
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		// 4、读取数据到缓冲区
		channel.read(buffer);
		buffer.flip();
		// 5、读取出缓冲区中的数据并输出即可
		String rs = new String(buffer.array(), 0, buffer.remaining());
		System.out.println(rs);

	}

	public static void copy() throws Exception {
		// 源文件
		File srcFile = new File("d:\\壁纸.jpg");
		File destFile = new File("d:\\壁纸new.jpg");
		// 得到一个字节字节输入流
		FileInputStream fis = new FileInputStream(srcFile);
		// 得到一个字节输出流
		FileOutputStream fos = new FileOutputStream(destFile);
		// 得到的是文件通道
		FileChannel isChannel = fis.getChannel();
		FileChannel osChannel = fos.getChannel();
		// 分配缓冲区
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while (true) {
			// 必须先清空缓冲然后再写入数据到缓冲区
			buffer.clear();
			// 开始读取一次数据
			int flag = isChannel.read(buffer);
			if (flag == -1) {
				break;
			}
			// 已经读取了数据 ，把缓冲区的模式切换成可读模式
			buffer.flip();
			// 把数据写出到
			osChannel.write(buffer);
		}
		isChannel.close();
		osChannel.close();
		System.out.println("复制完成！");
	}

	public static void test01() throws IOException {
		RandomAccessFile raf1 = new RandomAccessFile("1.txt", "rw");
		// 1. 获取通道
		FileChannel channel1 = raf1.getChannel();

		// 2. 分配指定大小的缓冲区
		ByteBuffer buf1 = ByteBuffer.allocate(100);
		ByteBuffer buf2 = ByteBuffer.allocate(1024);

		// 3. 分散读取
		ByteBuffer[] bufs = { buf1, buf2 };
		channel1.read(bufs);

		for (ByteBuffer byteBuffer : bufs) {
			byteBuffer.flip();
		}

		System.out.println(new String(bufs[0].array(), 0, bufs[0].limit()));
		System.out.println("-----------------");
		System.out.println(new String(bufs[1].array(), 0, bufs[1].limit()));

		// 4. 聚集写入
		RandomAccessFile raf2 = new RandomAccessFile("2.txt", "rw");
		FileChannel channel2 = raf2.getChannel();

		channel2.write(bufs);
	}

	public static void test02() throws Exception {
		// 1、字节输入管道
		FileInputStream is = new FileInputStream("data01.txt");
		FileChannel isChannel = is.getChannel();
		// 2、字节输出流管道
		FileOutputStream fos = new FileOutputStream("data03.txt");
		FileChannel osChannel = fos.getChannel();
		// 3、复制
		osChannel.transferFrom(isChannel, isChannel.position(), isChannel.size());
		isChannel.close();
		osChannel.close();
	}

	public static void test03() throws Exception {
		// 1、字节输入管道
		FileInputStream is = new FileInputStream("data01.txt");
		FileChannel isChannel = is.getChannel();
		// 2、字节输出流管道
		FileOutputStream fos = new FileOutputStream("data04.txt");
		FileChannel osChannel = fos.getChannel();
		// 3、复制
		isChannel.transferTo(isChannel.position(), isChannel.size(), osChannel);
		isChannel.close();
		osChannel.close();
	}
}
